﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Core.BaseCore.Extensions;
using Chinaer.Core.Helper;
using Chinaer.Model.Common;

namespace Chinaer.Bll
{
    public class ShangJia : Repository, IBll.IShangJia
    {
        IDao.IShangJiaDAO shangJiaDAO = new Dao.ShangJiaDAO();
        /// <summary>
        /// 根据商家名称获取商家DTO
        /// </summary>
        /// <param name="shangJiaName"></param>
        /// <returns></returns>
        public DTO.ShangJiaDTO GetByName(string shangJiaName)
        {
            Model.Entities.Model_ShangJia model_ShangJia = shangJiaDAO.GetByName(shangJiaName);
            DTO.ShangJiaDTO shangJiaDTO = new DTO.ShangJiaDTO();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_ShangJia, shangJiaDTO);
            return shangJiaDTO;
        }
        /// <summary>
        /// 通过apiToken获取商家实体
        /// </summary>
        /// <param name="apiToken"></param>
        /// <returns></returns>
        public DTO.ShangJiaDTO GetByApiToken(string apiToken)
        {
            string shangJiaName = Chinaer.Core.Helper.ApiTokenHelper.GetUserNameWithApiToken(apiToken);
            //通过apiToken获取商家用户名
            DTO.ShangJiaDTO shangJiaDTO = GetByName(shangJiaName);
            return shangJiaDTO;
        }
        /// <summary>
        /// 判断apiToken是否是有效的用户
        /// </summary>
        /// <param name="apiToken"></param>
        /// <returns></returns>
        public bool CheckApitokenIsUseful(string apiToken)
        {
            DTO.ShangJiaDTO shangJiaDTO = GetByApiToken(apiToken);
            if (shangJiaDTO == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public List<DTO.ShangJiaDTO> GetAllShangJias(string apiToken)
        {
            List<DTO.ShangJiaDTO> listShangJiaDTO = new List<DTO.ShangJiaDTO>();
            try
            {
                List<Model.Entities.Model_ShangJia> listModelShangJias = new List<Model.Entities.Model_ShangJia>();
                listModelShangJias = shangJiaDAO.GetAllShangJias();
                if (listModelShangJias != null)
                {
                    foreach (var item in listModelShangJias)
                    {
                        DTO.ShangJiaDTO shangJiaDTO = new DTO.ShangJiaDTO();
                        Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, shangJiaDTO);
                        listShangJiaDTO.Add(shangJiaDTO);
                    }
                }
            }
            catch (Exception ex)
            {
                WriteLog("ShangJiaBLL", "GetAllShangJias", Chinaer.Core.Helper.ExceptionMessageHelper.RollException(ex));
                listShangJiaDTO = null;
            }
            return listShangJiaDTO;
        }


        public Model.Common.JsonResult UpdateShangJia(DTO.ShangJiaDTO shangJiaDTO)
        {
            Model.Common.JsonResult jsonResult = new JsonResult();
            try
            {
                if (shangJiaDTO.CheckIsNull().Success == false)
                {
                    return shangJiaDTO.CheckIsNull();
                }

                if (!string.IsNullOrEmpty(shangJiaDTO.LoginName))
                {
                    if (shangJiaDAO.GetByName(shangJiaDTO.LoginName) != null)
                    {
                        return jsonResult.JsonResultExists();
                    }
                }
                Model.Entities.Model_ShangJia model_ShangJia = new Model.Entities.Model_ShangJia();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(shangJiaDTO, model_ShangJia);
                jsonResult.Success = shangJiaDAO.Update(model_ShangJia);
            }
            catch (Exception ex)
            {
                WriteLog("ShangJiaBLLError", "UpdateShangJia", Chinaer.Core.Helper.ExceptionMessageHelper.RollException(ex));
                jsonResult = ex.ExceptionJsonResult();
            }
            return jsonResult;
        }

        public Model.Common.JsonResult DeleteShangJia(DTO.ShangJiaDTO shangJiaDTO)
        {
            Model.Common.JsonResult jsonResult = new JsonResult();
            try
            {
                Model.Entities.Model_ShangJia model_ShangJia = new Model.Entities.Model_ShangJia();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(shangJiaDTO, model_ShangJia);
                jsonResult.Success = shangJiaDAO.Delete(model_ShangJia);
            }
            catch (Exception ex)
            {
                WriteLog("ShangJiaBLLError", "DeleteShangJiaError", Chinaer.Core.Helper.ExceptionMessageHelper.RollException(ex));
                jsonResult = ex.ExceptionJsonResult();
            }
            return jsonResult;
        }

        public Model.Common.JsonResult DeleteShangJiaByID(string shangJiaID, string apiToken)
        {
            Model.Common.JsonResult jsonResult = new JsonResult();
            try
            {
                Model.Entities.Model_ShangJia model_ShangJia = new Model.Entities.Model_ShangJia();              
                jsonResult.Success = shangJiaDAO.Delete(shangJiaID);
            }
            catch (Exception ex)
            {
                WriteLog("ShangJiaBLLError", "DeleteShangJiaByID", Chinaer.Core.Helper.ExceptionMessageHelper.RollException(ex));
                jsonResult = ex.ExceptionJsonResult();
            }
            return jsonResult;
        }
    }
}
